/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.ttvdroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;

import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView.BufferType;
import android.widget.Toast;

import org.me.schedule.Month;
import org.me.utils.CalendarProvider;
import org.me.utils.CalendarReader;

/**
 *
 * @author peterholtegaard@gmail.com
 */
public class TotalView extends Activity {

    private CalendarProvider calProv;
    public static final String PREFS_NAME = "preferences";
    static SharedPreferences settings;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        //initialisér applikation og en calendar provider

        super.onCreate(icicle);
        settings = getSharedPreferences(PREFS_NAME, 0);
        String uName = settings.getString("userName", "");
        String pWord = settings.getString("password", "");
        int chosenCalendar = (int) settings.getLong("calendar", 0);
        ContentResolver cr = getContentResolver();
        calProv = CalendarProvider.getInstance(cr);
        setContentView(R.layout.main);
        //opret objekter for gui elementer

        final EditText username = (EditText) findViewById(R.id.userName);
        username.setText(uName, BufferType.NORMAL);
        final EditText password = (EditText) findViewById(R.id.password);
        password.setText(pWord, BufferType.NORMAL);
        final Button okButton = (Button) findViewById(R.id.okbutton);
        okButton.setFocusable(true);
        //okButton.setFocusableInTouchMode(true);
        final Spinner spinner = (Spinner) findViewById(R.id.spinner);
        //fyld indhold på dropdown boks
        ArrayAdapter adapter = new ArrayAdapter(
                this, android.R.layout.simple_spinner_dropdown_item, calProv.getPhoneCalendars().toArray());
        spinner.setAdapter(adapter);
        spinner.setSelection(chosenCalendar);
        //actions for gui elementer
        username.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //sørg for du kun kan skrive en linje i feltet
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    v.focusSearch(View.FOCUS_DOWN);
                    password.requestFocus();
                    return true;
                }
                return false;
            }
        });


        password.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //sørg for du kun kan skrive en linje i feltet
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    v.focusSearch(View.FOCUS_DOWN);
                    okButton.requestFocus();
                    return true;
                }
                return false;
            }
        });
        final CheckBox checkbox = (CheckBox) findViewById(R.id.save_prefs);
        checkbox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // Perform action on clicks, depending on whether it's now checked
                if (((CheckBox) v).isChecked()) {
                    Toast.makeText(TotalView.this, "gemmer præferencer", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(TotalView.this, "gemmer ikke præferencer", Toast.LENGTH_SHORT).show();
                }
            }
        });



        okButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (checkbox.isChecked()) {
                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("userName", username.getText().toString());
                    editor.putString("password", password.getText().toString());
                    editor.putLong("calendar", spinner.getSelectedItemId());
                    editor.commit();
                }
                Intent i = new Intent(TotalView.this, SyncView.class);
                TotalView.this.startActivity(i);
            }
        });


    }

    public static SharedPreferences getPrefs() {
        return settings;

    }
}
/*             final ProgressDialog pd = ProgressDialog.show(TotalView.this,
"Title",
"Message",
true);

Thread t = new Thread(new Runnable() {


public void run() {
Month month = calProv.getTTVCalendar(username.getText().toString(),
password.getText().toString(), "06");
pd.dismiss();
onResult(month,
spinner.getSelectedItemId());
}

});

t.start();

if (checkbox.isChecked()) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("userName", username.getText().toString());
editor.putString("password", password.getText().toString());
editor.putLong("calendar", spinner.getSelectedItemId());
editor.commit();
}



}
});
 *
checkbox.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(TotalView.this, "gemmer præferencer", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(TotalView.this, "gemmer ikke præferencer", Toast.LENGTH_SHORT).show();
}
}
});

}
 */
/* public void onResult(final Month m,
final Long calID) {
String result = m.toString();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(result + "\nvil du skrive dette i din kalender?").setCancelable(false);
builder.setPositiveButton("ja", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {
calProv.addToCalendar(m, calID);
Toast.makeText(TotalView.this, "ok", Toast.LENGTH_LONG);
dialog.cancel();
}
});
builder.setNegativeButton("Nej", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create();
builder.show();


}*/
